package com.jsmtr.rms.utils.db;

import java.util.ArrayList;

/**
 * 行集合
 *
 * @author 陈宇翔
 * @version 1.0
 */
public class DbRowCollection extends ArrayList<DbRow> {
    private static final long serialVersionUID = -8940196742313994740L;
    ArrayList<String> colNameList = new ArrayList<String>();//列名

    public DbRowCollection() {
    }


    //---------------------------------------------初始化

    /**
     * 添加一行数据(系统初始化用)
     */
    protected boolean adds(DbRow rows) {
        try {
            super.add(rows);
            return true;
        } catch (Exception e) {
            System.out.println("DbRowCollection.adds(DbRow).err:" + e);
        }
        return false;
    }

    /**
     * 初始化列名元素(系统初始化用)
     */
    protected boolean addColName(String colName) {
        try {
            colNameList.add(colName);
            return true;
        } catch (Exception e) {
            System.out.println("DbRowCollection.addColName(colName).err:" + e);
        }
        return false;
    }

    //---------------------------------------------统计

    /**
     * 给出列长度
     *
     * @return
     */
    public int length() {
        return colNameList.size();
    }


    /**
     * 给出行长度
     *
     * @return
     */
    public int size() {
        return super.size();
    }

    //---------------------------------------------get

    /**
     * 根据指定索引得到列名
     */
    public String getColName(int index) {
        return colNameList.get(index).toString();
    }

    /**
     * 根据索引得到指定行
     */
    public DbRow get(int index) {
        if (index <= this.size()) {
            return super.get(index);
        } else {
            System.out.println("DbRowCollection.get(int).err:指定索引：" + index + "超出行最大索引位置:" + this.size());
        }
        return null;
    }

    //---------------------------------------------add

    /**
     * 增加一行
     */
    public boolean add(DbRow dbrowobj) {
        DbRow dbrow = (DbRow) dbrowobj.clone();
        dbrowobj.clear();
        if (dbrow.getTempList().size() != this.colNameList.size()) {
            System.out.println("DbRowCollection.add(DbRow).err:dbrow列长度" + dbrow.getTempList().size() + "不等于当前列长度" + this.colNameList.size());
            return false;
        }
        dbrow.setColName(this.colNameList);
        dbrow.flex();
        super.add(dbrow);
        return true;
    }

    /**
     * 在行集合指定索引位置增加一行
     */
    public void add(int index, DbRow dbrowobj) {
        DbRow dbrow = (DbRow) dbrowobj.clone();
        if (dbrow.getTempList().size() != this.colNameList.size()) {
            System.out.println("DbRowCollection.add(DbRow).err:dbrow列长度" + dbrow.getTempList().size() + "不等于当前列长度" + this.colNameList.size());
        }
        dbrow.setColName(this.colNameList);
        super.add(index, dbrow);
        dbrow.flex();
    }


    //---------------------------------------------set

    /**
     * 修改在指定索引位置行列名
     */
    protected void setColName(int index, String colName) {
        this.colNameList.set(index, colName);
    }


    /**
     * 修改最后行元素
     */
    boolean set(DbRow dbrow) {
        if (dbrow.getTempList().size() != this.colNameList.size()) {
            System.out.println("DbRowCollection.set(DbRow).err:dbrow列长度" + dbrow.getTempList().size() + "不等于当前列长度" + this.colNameList.size());
            return false;
        }
        dbrow.setColName(this.colNameList);
        dbrow.flex();
        super.set(this.size(), dbrow);
        return true;
    }

    /**
     * 修改指定索引的行元素
     */
    public DbRow set(int index, DbRow dbrowobj) {
        DbRow dbrow = (DbRow) dbrowobj.clone();
        if (dbrow.getTempList().size() != this.colNameList.size()) {
            System.out.println("DbRowCollection.set(DbRow).err:dbrow列长度" + dbrow.getTempList().size() + "不等于当前列长度" + this.colNameList.size());
            return null;
        }
        dbrow.setColName(this.colNameList);
        dbrow.flex();
        super.set(index, dbrow);
        return dbrow;
    }

    //---------------------------------------------remove

    /**
     * 移除指定索引的行对象
     */

    public DbRow removeDbRow(int index) {
        DbRow dbrow = (DbRow) super.get(index).clone();
        super.remove(index);
        return dbrow;
    }


}
